package com.opensky.qatest.ui.component;

import org.openqa.selenium.By;

import com.opensky.qatest.ui.page.ResetPasswordPage;
import com.opensky.qatest.ui.page.SalesPage;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * SignIn form on Login page
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.1
 * 
 */
public class LoginPageSignInForm implements IComponent {

    // ************************ Locators ************************

    private static final String SIGNIN_FORM = "//div[@id='main']//form[@class='auth']";

    private static final String EMAIL_FIELD = "email";

    private static final String PASSWORD_FIELD = "password";

    private static final String LOGIN_BTN = "//form[@class='auth']//input[@type='submit']";

    private static final String FORGOTPASS_LINK = "Forgot password?";

    private static final String NEVERREG_LINK = "Never created a password?";

    private static final String ALERT_MESSAGE_CONTAINER = "//div[@id='error-message']";

    private static final String ALERT_MESSAGE_TEXT = "We didn't recognize the email or password you entered. Please try again.";

    private final WebDriverWrapper driver;

    public LoginPageSignInForm(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    public SalesPage signIn(String email, String password) {

	SalesPage salesPage = new SalesPage(driver);

	driver.waitAndSendKeys("EMAIL_FIELD", By.id(EMAIL_FIELD), email);
	driver.waitAndSendKeys("PASSWORD_FIELD", By.id(PASSWORD_FIELD), password);
	driver.waitAndClick("LOGIN_BTN", By.xpath(LOGIN_BTN));

	if (driver.waitTimeAndDefineElementWithTextPresent("ALERT_MESSAGE_CONTAINER", By.xpath(ALERT_MESSAGE_CONTAINER),
		ALERT_MESSAGE_TEXT, 10)) {
	    return null;
	}

	return salesPage.isPageLoaded() ? salesPage : null;
    }

    public ResetPasswordPage clickResetPassword() {

	ResetPasswordPage resetPasswordPage = new ResetPasswordPage(driver);
	driver.waitAndClick("FORGOTPASS_LINK", By.linkText(FORGOTPASS_LINK));

	return resetPasswordPage.isPageLoaded() ? resetPasswordPage : null;
    }

    // ************************ Validations **************************

    @Override
    public boolean isComponentLoaded() {
	return driver.waitAndVerifyElementPresent("SIGNIN_FORM", By.xpath(SIGNIN_FORM));
    }

    public boolean isSignInFormElementsPresent() {

	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("EMAIL_FIELD", By.id(EMAIL_FIELD)),
		driver.waitAndVerifyElementPresent("PASSWORD_FIELD", By.id(PASSWORD_FIELD)),
		driver.waitAndVerifyElementPresent("LOGIN_BTN", By.xpath(LOGIN_BTN)),
		driver.waitAndVerifyElementPresent("FORGOTPASS_LINK", By.linkText(FORGOTPASS_LINK)),
		driver.waitAndVerifyElementPresent("NEVERREG_LINK", By.linkText(NEVERREG_LINK)));
    }

    public boolean isAlertMessagePresent() {
	return driver.waitAndVerifyElementWithTextPresent("ALERT_MESSAGE_CONTAINER", By.xpath(ALERT_MESSAGE_CONTAINER), ALERT_MESSAGE_TEXT);
    }

    // ************************ GET/SET methods **************************

    @Override
    public String getName() {
	return "Login page sigIn form";
    }
}
